<?php
    
    function generateShortDesc ( $p_news_content , $number =100 ){
        $v_desc = '';
        if ( $p_news_content ){
            $v_desc = cut_str($p_news_content,'','.');
        }
        return $v_desc;
    }


    function get_news_image_file($p_news_id, $p_image_num, $p_image_size) {
        global $v_thum_image_size, $v_mini_image_size;
        $v_thum_image_size = NEWS_THUM_SIZE;
        $v_mini_image_size = MINI_THUM_SIZE;
        $v_normal_image_size = NORMAL_THUM_SIZE;
        switch ($p_image_size) {
            case "thum";
                $v_image_size = $v_thum_image_size;
                break;
            case "mini";
                $v_image_size = $v_mini_image_size;
                break;
            case "normal";
                $v_image_size = $v_normal_image_size;
                break;
            default;
                $p_image_size = "thum";
                $v_image_size = $v_thum_image_size;
        }
        $v_news_image_file = "lib/images/news/" . $p_news_id . "_" . $p_image_num . ".jpg";
        $v_news_thum_image_file = "lib/images/news/" . $p_image_size . $p_news_id . "_" . $p_image_num . ".jpg";

        if (!file_exists($v_news_image_file)) {
            $v_image = get_news_image($p_news_id, $p_image_num);
            if ($v_image) {
                $handle = @fopen($v_news_image_file, "w");
                @fwrite($handle, $v_image);
            } else {
                $v_news_image_file = "";
            }
        }
        if (!file_exists($v_news_image_file)) {
            $v_news_image_file = "";
            $v_news_thum_image_file = "";
        } else {
            if (!file_exists($v_news_thum_image_file)) {
                if ($p_image_size == "thum") {
                    images_resize($v_image_size, 80, $v_news_image_file, $v_news_thum_image_file);
                } else {
                    images_resize($v_image_size, $v_image_size, $v_news_image_file, $v_news_thum_image_file);
                }
            }
        }
        return $v_news_thum_image_file;
    }

    function get_news_image($p_news_id, $p_image_num) {
        $rs = mysql_query(" SELECT IMAGE_DATA  FROM TB_NEWS_IMAGES WHERE NEWS_ID='" . $p_news_id . "' AND IMAGE_NUM ='" . $p_image_num . "' ") or die(mysql_error());
        if (!mysql_num_rows($rs))
            return 0;
        $arr = mysql_fetch_array($rs, MYSQL_NUM);
        return $arr[0];
    }

    function get_singer_news($p_news_id) {
        $rs = mysql_query("
    		SELECT
    			NEWS_ID 
    			,NEWS_CATEGORY 
    			,NEWS_TITLE 
    			,NEWS_DESCRIPTION
    			,NEWS_CONTENT
    			,NEWS_TIME
    			,NEWS_ORDER
    			,NEWS_TYPE
    			,NEWS_HITCOUNT
    			,NEWS_KEY
    			,NEWS_TITLE_EN
    			,NEWS_DESCRIPTION_EN
    			,NEWS_CONTENT_EN
    		FROM TB_NEWS
    		WHERE NEWS_ID = '" . $p_news_id . "'	
    	") or die(mysql_error());
        return $rs;
    }

    function get_news_thum($v_news_id, $v_size="mini") {
        $v_retva = "";
        $rs = mysql_query("SELECT IMAGE_NUM FROM TB_NEWS_IMAGES WHERE NEWS_ID = '" . $v_news_id . "' ORDER BY IMAGE_NUM ASC LIMIT 1 ") or die(mysql_error());
        if ($arr = mysql_fetch_array($rs, MYSQL_NUM)) {
            $v_image_num = $arr[0];
            $v_retva = get_news_image_file($v_news_id, $v_image_num, $v_size);
        } else {
            $v_retva = "";
        }
        return $v_retva;
    }

    function get_all_news($p_where_clause, $p_num_row, $p_offset, $p_order_clause="NEWS_TIME DESC, NEWS_ORDER DESC, NEWS_ID DESC ") {
        $rs = mysql_query("
    		SELECT 
    			NEWS_ID 
    			,NEWS_CATEGORY 
    			,NEWS_TITLE 
    			,NEWS_DESCRIPTION
    			,NEWS_CONTENT
    			,NEWS_TIME
    			,NEWS_ORDER
    			,NEWS_TYPE
    			,NEWS_STATUS
    			,NEWS_HITCOUNT
    			,NEWS_KEY
    			,NEWS_TITLE_EN
    			,NEWS_DESCRIPTION_EN
    			,NEWS_CONTENT_EN
    							
    		FROM TB_NEWS
    		WHERE 1 " . $p_where_clause . "	
    		ORDER BY " . $p_order_clause . "
    		LIMIT " . $p_offset . "," . $p_num_row . "
    	") or die(mysql_error());
        return $rs;
    }

    function get_all_news_images($p_news_id) {
        $rs = mysql_query("
    		SELECT IMAGE_NUM FROM TB_NEWS_IMAGES WHERE NEWS_ID = '" . $p_news_id . "'
    	") or die(mysql_error());
        return $rs;
    }

    function write_news_images($p_news_id, $p_image_num) {
        $v_news_image_file = "lib/images/news/" . $p_news_id . "_" . $p_image_num . ".jpg";
        if (!file_exists($v_news_image_file)) {
            $v_image = get_news_image($p_news_id, $p_image_num);
            if ($v_image) {
                $handle = @fopen($v_news_image_file, "w");
                @fwrite($handle, $v_image);
            } else {
                $v_news_image_file = "";
            }
        }
    }

    function delete_news_image($p_news_id, $p_image_num) {
        mysql_query("DELETE FROM  TB_NEWS_IMAGES WHERE NEWS_ID = '" . $p_news_id . "' AND IMAGE_NUM ='" . $p_image_num . "' ") or die(mysql_error());
        @unlink("lib/images/news/" . $p_news_id . "_" . $p_image_num . ".jpg");
        @unlink("lib/images/news/thum" . $p_news_id . "_" . $p_image_num . ".jpg");
    }

    function delete_news($p_news_id) {
        $rs = mysql_query(" DELETE FROM TB_NEWS WHERE NEWS_ID='" . $p_news_id . "' ") or die(mysql_error());
        $rs = mysql_query("	SELECT IMAGE_NUM FROM TB_NEWS_IMAGES WHERE NEWS_ID = '" . $p_news_id . "'	") or die(mysql_error());
        while ($arr = mysql_fetch_array($rs, MYSQL_NUM)) {
            delete_news_image($p_news_id, $arr[0]);
        }
    }
    function get_news_id($p_news_key) {
        $rs = mysql_query("SELECT NEWS_ID FROM TB_NEWS WHERE NEWS_KEY = '" . $p_news_key . "'") or die(mysql_error());
        if ($arr = mysql_fetch_array($rs, MYSQL_NUM))
            return $arr[0];
        return 0;
    }

    function get_news_key($p_news_id) {
        if ($p_news_id == 0)
            return "";
        $query = " SELECT NEWS_TITLE,NEWS_CATEGORY FROM TB_NEWS WHERE NEWS_ID = '" . $p_news_id . "'";
        $result = mysql_query($query) or die("Query failed : " . mysql_error());
        $news_infor = mysql_fetch_array($result, MYSQL_NUM);
        $v_ret_val = 'khuyen-mai/' . strtolower(remove_invailid_char_qa(convert_utf8_to_kd($news_infor[0])));
        return $v_ret_val;
    }
    function insertNews(
        $p_news_category
            ,$p_news_title
            ,$p_news_description
            ,$p_news_content
            ,$p_news_time
            ,$p_news_status
            ,$p_news_order
            ,$p_news_hitcount
            ,$p_news_src
            ,$p_news_key
            ,$p_account_id
    
    ){
        mysql_query("
            INSERT INTO TB_NEWS(
                NEWS_CATEGORY
                    ,NEWS_TITLE
                    ,NEWS_DESCRIPTION
                    ,NEWS_CONTENT
                    ,NEWS_TIME
                    ,NEWS_STATUS
                    ,NEWS_ORDER
                    ,NEWS_HITCOUNT
                    ,NEWS_SRC
                    ,NEWS_KEY
                    ,ACCOUNT_ID
    
            )VALUES(
                '".$p_news_category."'
                    ,'".$p_news_title."'
                    ,'".$p_news_description."'
                    ,'".$p_news_content."'
                    ,'".$p_news_time."'
                    ,'".$p_news_status."'
                    ,'".$p_news_order."'
                    ,'".$p_news_hitcount."'
                    ,'".$p_news_src."'
                    ,'".$p_news_key."'
                    ,'".$p_account_id."'
    
            )   
        ")or die(mysql_error());
        return mysql_insert_id();
    }
    
    function updateNews(
        $p_news_id
            ,$p_news_category
            ,$p_news_title
            ,$p_news_description
            ,$p_news_content
            ,$p_news_time
            ,$p_news_status
            ,$p_news_order
            ,$p_news_hitcount
            ,$p_news_src
            ,$p_news_key
            ,$p_account_id
    
    ){
        mysql_query("
            UPDATE TB_NEWS 
            SET
                NEWS_CATEGORY='".$p_news_category."'
                    ,NEWS_TITLE='".$p_news_title."'
                    ,NEWS_DESCRIPTION='".$p_news_description."'
                    ,NEWS_CONTENT='".$p_news_content."'
                    ,NEWS_TIME='".$p_news_time."'
                    ,NEWS_STATUS='".$p_news_status."'
                    ,NEWS_ORDER='".$p_news_order."'
                    ,NEWS_HITCOUNT='".$p_news_hitcount."'
                    ,NEWS_SRC='".$p_news_src."'
                    ,NEWS_KEY='".$p_news_key."'
                    ,ACCOUNT_ID='".$p_account_id."'
    
            WHERE NEWS_ID =  '".$p_news_id."'
        ");
    }

    function getAllNews($p_where_clause,$p_offset,$p_num_row,$p_order_by=" NEWS_ID DESC "){
        $rs = mysql_query("
        SELECT 
            NEWS_ID
                ,NEWS_CATEGORY
                ,NEWS_TITLE
                ,NEWS_DESCRIPTION
                ,NEWS_CONTENT
                ,NEWS_TIME
                ,NEWS_STATUS
                ,NEWS_ORDER
                ,NEWS_HITCOUNT
                ,NEWS_SRC
                ,NEWS_KEY
                ,ACCOUNT_ID

        FROM TB_NEWS    
        WHERE 1 ".$p_where_clause."     
        ORDER BY ".$p_order_by."
        LIMIT ".$p_offset.",".$p_num_row."
        ")or die(mysql_error());
        return $rs;
    }

    function getSingerNews($p_news_id){
        $rs = mysql_query("
        SELECT 
            NEWS_ID
                ,NEWS_CATEGORY
                ,NEWS_TITLE
                ,NEWS_DESCRIPTION
                ,NEWS_CONTENT
                ,NEWS_TIME
                ,NEWS_STATUS
                ,NEWS_ORDER
                ,NEWS_HITCOUNT
                ,NEWS_SRC
                ,NEWS_KEY
                ,ACCOUNT_ID

        FROM TB_NEWS    
        WHERE NEWS_ID =  '".$p_news_id."'       
        ")or die(mysql_error());
        return $rs;
    }

    function getOwnerNews( $p_news_id ){
        $rs = getSingerNews( $p_news_id );
        if ( $arr = mysql_fetch_assoc($rs) ){
            return $arr['ACCOUNT_ID'];
        }
        return '';
    }
?>
